﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace MDS
{
    public class GZLXInterestProvider : IInterestProvider
    {
        private GZLXInterestProviderConfig _config;
        private Dictionary<string, double> _interestDic = new Dictionary<string, double>();
        private bool _isReading = false;
        public GZLXInterestProvider(GZLXInterestProviderConfig config)
        {
            Contract.Requires(config != null);
            this._config = config;
        }



        public double GetAccuredInterest(string stkId)
        {
            if (this._interestDic.ContainsKey(stkId))
                return this._interestDic[stkId];
            else
                return 0.0;
        }

        public void RefreshData()
        {
            if (this._isReading)
                return;
            this._isReading = true;
            try
            {
                read(this._config.DBFFileFullPath);
            }
            catch (Exception ex)
            {
                Log.Debug(ex);
            }
            finally
            {
                this._isReading = false;
            }
        }

        private void read(string fileName)
        {
            List<GZLXDBFRecord> records = DBFHelper.ReadRecords<GZLXDBFRecord>(fileName);
            if (records != null)
            {
                foreach (var record in records)
                {
                    this._interestDic[record.StkId] = record.AccuredInterest;
                }
            }
        }
    }
}
